package org.one.stone.soup.grfxML;

import org.one.stone.soup.mjdb.data.field.DataLinker;
import org.one.stone.soup.mjdb.data.field.Field;
import org.one.stone.soup.mjdb.data.field.LockException;
import org.one.stone.soup.xml.XmlElement;
  /*
 * Copyright 2000-2001 Wet-Wired.com Ltd.,
 * Portsmouth England
 * All rights reserved.
 */

/**
	*
	* @author Nik Cross
*/
public class DataRectangle extends DataGeometry implements ITag {

	public static String DEFAULT_ID = "_Rectangle";

	DataPoint start = new DataPoint();
	DataPoint startView = new DataPoint();
	DataPoint end = new DataPoint(TagModifier.POINT_TYPE_POINT,20,10);
	DataPoint endView = new DataPoint(TagModifier.POINT_TYPE_POINT,20,10);

	DataPoints _points = new DataPoints();
	DataPoints _pointsView = new DataPoints();
	GeometryPolygon _polygon = null;
	DataTranslation _translation;
public DataRectangle() {
	try{
		setId(DEFAULT_ID,this);
	}
	catch(LockException le){} //Dummy catch
}
public void buildFromgrfxML(XmlElement xml)
{

	String name = xml.getAttributeValueByName(Token.ID);
	if(name!=null)
	{
		try{
			setId(name,this);
		}
		catch(LockException le){} //Dummy catch
	}

	XmlElement element;

	for(int loop=0;loop<xml.getElementCount();loop++)
	{
		element = xml.getElementByIndex(loop);
		if(element == null)
			continue;

		String eleName = element.getName();

		if(eleName.equals(Token.START))
		{
			start = new DataPoint(TagModifier.POINT_TYPE_POINT);
			start.buildFromgrfxML(element,TagModifier.POINT_TYPE_POINT);
		}
		else if(eleName.equals(Token.END))
		{
			end = new DataPoint(TagModifier.POINT_TYPE_POINT);
			end.buildFromgrfxML(element,TagModifier.POINT_TYPE_POINT);
		}
		else if(super.buildFromElement(element)==false && !NameHelper.isSystemName(element.getName()))
		{
			GrfxMLException.warning( GrfxMLException.WARNING_UNRECOGNISED_TAG_CODE ,new Object[]{eleName,Token.RECTANGLE,""+element.getStartLineNo()} );
		}
	}
}
public void copyFromgrfxML(XmlElement xml)
{
	XmlElement element;

	for(int loop=0;loop<xml.getElementCount();loop++)
	{
		element = xml.getElementByIndex(loop);
		if(element == null)
			continue;

		String eleName = element.getName();

		if(eleName.equals(Token.START))
		{
			start.copyFromgrfxML(element,TagModifier.POINT_TYPE_POINT);
		}
		else if(eleName.equals(Token.END))
		{
			end.copyFromgrfxML(element,TagModifier.POINT_TYPE_POINT);
		}
		else if(super.copyFromElement(element)==false)
		{
			GrfxMLException.warning( GrfxMLException.WARNING_UNRECOGNISED_TAG_CODE ,new Object[]{eleName,Token.RECTANGLE,""+element.getStartLineNo()} );
		}
	}
}
public DataGeometry getClone() {

	DataRectangle newRectangle = new DataRectangle();

	super.getClonedElements(newRectangle);

	newRectangle.start = start.getClone();
	newRectangle.end = end.getClone();

	return newRectangle;
}
public DataGeometry getClone(String path) {

	DataRectangle newRectangle = new DataRectangle();

	GrfxMLStoreProcessor.setCloneName( path,this,newRectangle );

	super.getClonedElements(path,newRectangle);

	newRectangle.start = start.getClone(path);
	newRectangle.end = end.getClone(path);

	return newRectangle;
}
/**
 *
 * @return org.one.stone.soup.grfxML.DataPoint
 */
public DataPoint getEnd() {
	return end;
}
public Geometry getGeometry(GrfxMLEngine engine)
{
	if(_polygon==null && _translation!=null && color!=null)
	{
		GeometryProcessor.process(this);
		_polygon = new GeometryPolygon(
				engine,
				_translation.window,
				bounds,
				_translation.visibleView,
				transparency,
				color,
				filled,
				DataState.TRUE,
				_translation.positionView,
				offsetView,
				_pointsView,
				new DataSize(1)
				);
		if( !NameHelper.isSystemName(getId()) )
		{
			_polygon.setId(getId());
		}
	}

/*	if(start.hasChanged() || end.hasChanged())
	{
		grfxMLGeometryProcessor.process(this);
	}*/

	return(_polygon);
}
public java.awt.Polygon getPolygon() {
	return _polygon.polygon;
}
/**
 *
 * @return org.one.stone.soup.grfxML.DataPoint
 */
public DataPoint getStart() {
	return start;
}
public void register(DataLinker store) {

	super.register(store);

	try{
		store.add(start,this);
	}
	catch(LockException le)
	{}//Dummy catch

	try{
		store.add(end,this);
	}
	catch(LockException le)
	{}//Dummy catch
}
public void replace(Field oldObj, Field newObj) {
	replace(oldObj, newObj, false);
}
public void replace(Field oldObj, Field newObj, boolean recursive) {

	_polygon=null;

	super.replace(oldObj,newObj);

	if(start==oldObj)
		start=(DataPoint)newObj;

	if(end==oldObj)
		end=(DataPoint)newObj;
}
public void resetChanged() {

	super.resetChanged();

	start.resetChanged();
	startView.resetChanged();

	end.resetChanged();
	endView.resetChanged();
}
/**
 *
 * @param newEnd org.one.stone.soup.grfxML.DataPoint
 */
protected void setEnd(DataPoint newEnd) {
	end = newEnd;
}
/**
 *
 * @param newStart org.one.stone.soup.grfxML.DataPoint
 */
protected void setStart(DataPoint newStart) {
	start = newStart;
}
public String togrfxML(String name) {
	String xml = "<" + name;

	if(!NameHelper.isSystemName(getId()))
	{
		xml += " id=\""+getId()+"\"";
	}

	xml += ">";
	xml += super.togrfxML(name);
	xml += start.togrfxML(Token.START);
	xml += end.togrfxML(new DataPoint(20,10),Token.END);
	xml += "</" + name + ">\n";
	return xml;
}
public String toString()
{
	if(GrfxMLEngine.DEBUG)
		return(togrfxML( DEFAULT_ID.substring(1) ));

	String xml = "<"+DEFAULT_ID.substring(1)+"/>";

	return xml;
}
public void translate(DataTranslation t)
{

 	if(_translation==null)
		_translation=t;

	try{
		GeometryProcessor.process(this);

		GrfxMLProcessor.translate(_points,TagModifier.POINT_TYPE_POINT,t,_pointsView);
	}
	catch(LockException le)
	{
		le.printStackTrace();
	}

	super.translate(t);
}
}
